package view;

import service.ReadJobInfoService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;

public class ReadJobFrame  extends JFrame {
    private ReadJobInfoService readJobInfoService = new ReadJobInfoService();
    public ReadJobFrame() throws HeadlessException {
        this.setTitle("爬取数据");

        this.setBounds(400,300,800,100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER);
        this.setLayout(flowLayout);
        Dimension dimensionLabel = new Dimension(50,30);
        Dimension dimensionField = new Dimension(240,30);
        JLabel jLabelCity = new JLabel("城市");
        jLabelCity.setPreferredSize(dimensionLabel);
        JTextField jTextFieldCity = new JTextField();
        jTextFieldCity.setPreferredSize(dimensionField);

        JLabel jLabelJob = new JLabel("职位");
        jLabelJob.setPreferredSize(dimensionLabel);
        JTextField jTextFieldJob = new JTextField();
        jTextFieldJob.setPreferredSize(dimensionField);
        JButton jButton = new JButton("搜索");
        jButton.setPreferredSize(dimensionLabel);

        this.add(jLabelCity);
        this.add(jTextFieldCity);
        this.add(jLabelJob);
        this.add(jTextFieldJob);
        this.add(jButton);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //读取数据并插入数据库
                String cityNum = jTextFieldCity.getText();
                String job = jTextFieldJob.getText();
                try {
                    readJobInfoService.readJobData(cityNum,job,1);
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                } catch (ClassNotFoundException classNotFoundException) {
                    classNotFoundException.printStackTrace();
                }

            }
        });

    }


}
